﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Navigation;

using EWSUtil = Microsoft.Samples.Office.UnifiedCommunications.Gadget.EWSUtility;
using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Events;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Application.Current.DispatcherUnhandledException += 
                new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
        }

        void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            if (e.Exception.InnerException.Message.IndexOf(ConfigurationManager.AppSettings["EWSUnauthorizedException"]) >= 0)
            {
                e.Handled = true;
                GadgetEventManager.Instance.OnEWSUnauthorizedEvent(new EventArgs());
            }
            else
            {
                if (Convert.ToBoolean(ConfigurationManager.AppSettings["EatUnhandledExceptions"]) == true)
                {
                    e.Handled = true;
                }
            }
        }
    }
}